# -*- coding: utf-8 -*-
'''
Created on Apr 20, 2012

@author: LONG HOANG GIANG
'''
import sys, os
sys.path.append(os.path.expanduser('/home5/vietcntt/longhoanggiang/python'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
from CrawlerLib import Http, commonlib
import datetime
import re
import simplejson as json
import time

html_cfg = '''<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="content-type" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="format-detection" content="telephone=no" />
<title>#TITLE#</title>
<style type="text/css">
body {
    text-align: justify;
    text-justify: newspaper;
    font-size: 1.2em;
}
</style>
</head>
<body>
#CONTENT#
</body>
</html>
</html>'''

class Wattpad():
    
    output = '/wattpad/'
    title = "GQ8891 - UNGDUNGVIET"
    
    def set_title(self, title):
        self.title = title
    
    def set_output(self, output):
        if output[-1] != '/': output += '/'
        self.output = output
    
    def process(self, storyId, start, end, spos=0, prefix=''):
        if not os.path.exists(self.output): os.makedirs(self.output, 0777)
        for ipage in range(start, end + 1):
            for itry in range (0, 5):
                url = 'http://www.wattpad.com/apiv2/storytext?id={0}&page={1}'.format(storyId, ipage)
                filePath = '{0}{1}{2}.html'.format(self.output, prefix, spos + ipage)
                json_html = Http.getHtml(url)
                
                
                
                if json_html != '' and json_html != 'false' and json_html != None:
                    jsondata = json.loads(json_html)
                    html = jsondata['text']
                    if type(html).__name__ == 'unicode': html = html.encode('utf-8')
                    ohtml = re.sub(r"(#TITLE#)", self.title, html_cfg)
                    html = re.sub(r"(#CONTENT#)", html, ohtml)
                    commonlib.file_put_contents(filePath, html)
                    break
                print '>> retry {0} get {1}'.format(itry, url)
                time.sleep(1)
        return
    
if __name__ == '__main__':
    
    wp = Wattpad()
    wp.set_output('/wattpad/')
    wp.process('2360177', 125, 158, 0, '')
    
    print '>> Finished at {0}'.format(datetime.datetime.now())
    os._exit(1)
    
    